package com.gklee.regionselector;

import com.google.gson.Gson;

import java.util.ArrayList;

public class DataProvider
{
    public static final ArrayList<SelectBean> SELECT_BEANS = new ArrayList<>();
    private static int index = 0;

    static String json = " \n" +
            " {   \n" +
            "      \"id\": 1,\n" +
            "      \"class_name\": \"场外设备\",\n" +
            "      \"children\": [\n" +
            "        {\n" +
            "          \"id\": 3,\n" +
            "          \"class_name\": \"跑道设备\",\n" +
            "          \"children\": [\n" +
            "            {\n" +
            "              \"id\": 7,\n" +
            "              \"class_name\": \"二级分类1\",\n" +
            "              \"children\": [\n" +
            "                {\n" +
            "                  \"id\": 13,\n" +
            "                  \"class_name\": \"锤子2\"\n" +
            "                },\n" +
            "                {\n" +
            "                  \"id\": 14,\n" +
            "                  \"class_name\": \"锤子\"\n" +
            "                }\n" +
            "              ]\n" +
            "            },\n" +
            "            {\n" +
            "              \"id\": 8,\n" +
            "              \"class_name\": \"二级分类2\"\n" +
            "            },\n" +
            "            {\n" +
            "              \"id\": 11,\n" +
            "              \"class_name\": \"\"\n" +
            "            },\n" +
            "            {\n" +
            "              \"id\": 12,\n" +
            "              \"class_name\": \"道场大灯\"\n" +
            "            }\n" +
            "          ]\n" +
            "        },\n" +
            "        {\n" +
            "          \"id\": 4,\n" +
            "          \"class_name\": \"跑道设备2\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"id\": 5,\n" +
            "          \"class_name\": \"跑道设备3\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"id\": 6,\n" +
            "          \"class_name\": \"跑道设备4\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"id\": 10,\n" +
            "          \"class_name\": \"跑道设备5\"\n" +
            "        }\n" +
            "      ]\n" +
            "    }\n" +
            "  ";


    public static ArrayList<SelectBean> resetSelectBean()
    {
        Gson gson = new Gson();
        PVGSuppliesBean dataBean = gson.fromJson(json, PVGSuppliesBean.class);
        // 第一层
        SelectBean bean = getOutBean(dataBean.getClass_name());
        bean.setId(dataBean.getId());
        for (PVGSuppliesBean.ParentBean parentBean : dataBean.getChildren())
        {
            addInner1Data(bean, parentBean);
        }

        SELECT_BEANS.add(bean);
        return SELECT_BEANS;
    }

    /**
     * 添加第一层数据
     *
     * @param selectBean
     * @param parentBean
     */
    public static void addInner1Data(SelectBean selectBean, PVGSuppliesBean.ParentBean parentBean)
    {
        SelectBean bean = selectBean.addChild(parentBean.getClass_name(), parentBean.getId());
        if (parentBean.getChildren() != null)
        {
            for (PVGSuppliesBean.ParentBean.ChildrenBean childrenBean : parentBean.getChildren())
            {
                addInner2Data(bean, childrenBean);
            }
        }
    }

    /**
     * 添加第二层
     *
     * @param selectBean
     * @param parentBean
     */
    public static void addInner2Data(SelectBean selectBean, PVGSuppliesBean.ParentBean.ChildrenBean parentBean)
    {
        SelectBean bean = selectBean.addChild(parentBean.getClassName(), parentBean.getId());
        if (parentBean.getChildren() != null)
        {
            for (PVGSuppliesBean.ParentBean.ChildrenBean.ChildrenBean1 childrenBean : parentBean.getChildren())
            {
                addInner3Data(bean, childrenBean);
            }
        }
    }

    /**
     * 添加第三层
     *
     * @param selectBean
     * @param childrenBean
     */
    public static void addInner3Data(SelectBean selectBean, PVGSuppliesBean.ParentBean.ChildrenBean.ChildrenBean1 childrenBean)
    {
        SelectBean bean = selectBean.addChild(childrenBean.getClassName(), childrenBean.getId());
    }


    /**
     * 获取外层的Bean
     *
     * @param name
     * @return
     */
    private static SelectBean getOutBean(String name)
    {
        SelectBean bean = new SelectBean();
        bean.setName(name);
        bean.setIndex(index++);
        return bean;
    }

}
